﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EntitySpaces.Interfaces;
using yncTech.BusinessObjects;
using yncTech.Common;
using System.Data;

public partial class Admin_FinMov_FinMov001f :AdminBasePage
{
    decimal shipID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[FinShippingMetadata.ColumnNames.ShipID] != null)
        {
            shipID = decimal.Parse(Request.QueryString[FinShippingMetadata.ColumnNames.ShipID]);
        }
        if (!IsPostBack)
        {
            FinShipping fs = new yncTech.BusinessObjects.FinShipping();
            fs.LoadByPrimaryKey(shipID);

           
        }

    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        GridDataBind(e.NewPageIndex);
    }

    void GridDataBind(int page)
    {

        VBoxReceiptsQuery q1 = new VBoxReceiptsQuery();
        FinShippingBoxesQuery q2 = new FinShippingBoxesQuery();

        q1.SelectAll().Select(q2.ShippingBoxesID,q2.PackageNo);
        q1.InnerJoin(q2).On(q1.BoxID == q2.BoxID);
        q1.Where(q2.ShipID == shipID);

        pageGrid.DataSource = q1.LoadDataTable();
        pageGrid.CurrentPage = page;
        pageGrid.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";

            List<decimal> boxIDs = new List<decimal>();
            using (esTransactionScope tr = new esTransactionScope())
            {
                FinShipping fs = new FinShipping();
                fs.LoadByPrimaryKey(shipID);

                fs.UpdatedBy = fs.Check1 = LoginUserInfo.CurrentUser.UserName;
                fs.UpdatedDttm = fs.CheckDttm1 = DateTime.Now;

                fs.Save();
                FinShippingBoxes fsb = new FinShippingBoxes();
                foreach (RepeaterItem item in pageGrid.Items)
                {
                   
                        Label lbReceipt = (Label)item.FindControl("lbShipBoxID");
                        TextBox txtPackageNo = (TextBox)item.FindControl("txtPackNo");
                        var shipboxId = decimal.Parse(lbReceipt.Text);
                        fsb.LoadByPrimaryKey(shipboxId);

                        fsb.PackageNo = txtPackageNo.Text;
                        fsb.UpdatedBy = LoginUserInfo.CurrentUser.UserName;
                        fsb.UpdatedDttm = DateTime.Now;
                        fsb.Save();
                   
                }

                tr.Complete();

                WebHelper.CloseDialog();
            }

        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }
    protected string GetBoxnoOrBarcodeText()
    {
        FinShippingQuery q1 = new yncTech.BusinessObjects.FinShippingQuery("q1");
        BFThirdUnitQuery q2 = new yncTech.BusinessObjects.BFThirdUnitQuery("q2");
        q1.LeftJoin(q2).On(q1.ToDestination == q2.ThirdUnitID);
        q1.Select(q2.ThirdType);
        q1.Where(q1.ShipID == shipID);
        DataTable data = q1.LoadDataTable();
        if (data != null && data.Rows.Count > 0 && data.Rows[0]["ThirdType"].ToString() != "JGLX003")
        {
            return "打包箱号";
        }
        else
        {
            return "外仓条形码";
        }

    }
}